package meijuli.com.join51.business.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.ResObj;
import com.jhx.common.widget.CommonDialog;
import com.jhx.common.widget.LoadingDialog;
import meijuli.com.join51.business.utils.PermissionUtils;
import com.jhx.common.utils.ToastUtil;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;
import retrofit2.Response;

/**
 * 请描述使用该类使用方法！！！
 *
 * @author wenqin 2017-04-13 16:27
 */

public class BaseActivity extends AppCompatActivity{
    private OnActivityDispatchTouchEventListener onActivityDispatchTouchEventListener;
    private FragmentManager mFragmentManager;
    protected LoadingDialog mLoadingDialog;
    protected CommonDialog mDialog = null;
    /**
     * 确认对话框
     */
    private AlertDialog mYesNoDialog;
    protected String TAG = getClass().getName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.black));
        }
    }

    protected void addFragment(Fragment fragment) {
        mFragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        ft.add(R.id.content_view, fragment, fragment.getClass().getName()).commitAllowingStateLoss();
    }

    protected Fragment findFragmentByTag(String tag) {
       return mFragmentManager.findFragmentByTag(tag);
    }

    /**
     * 根据tag显示fragment
     * @param tag
     */
    protected void showFragment(String tag) {
        List<Fragment> fragmentList = mFragmentManager.getFragments();
        for(Fragment fragment : fragmentList) {
            if(fragment.getClass().getName().equals(tag)) {
                mFragmentManager.beginTransaction().show(fragment).commit();
            } else {
                mFragmentManager.beginTransaction().hide(fragment).commit();
            }
        }
    }

    protected void showLoadingDialog() {
        if(mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.dismiss();
        mLoadingDialog.show();
    }

    protected void dismissDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    /**
     * 设置触摸事件分发监听器
     */
    public void setOnActivityDispatchTouchEventListener(OnActivityDispatchTouchEventListener onActivityDispatchTouchEventListener) {
        this.onActivityDispatchTouchEventListener = onActivityDispatchTouchEventListener;
    }

    /**
     * 触摸事件分发接口
     */
    public interface OnActivityDispatchTouchEventListener {
        boolean onActivityDispatchTouchEvent(MotionEvent ev);
    }


    public AlertDialog showYesNoDialog(String positiveButton, String negativeButton, String text, DialogInterface.OnClickListener clickListener) {
        if (null == mYesNoDialog) {
            mYesNoDialog = new AlertDialog.Builder(this)
                    .setCancelable(false)
                    .create();
        }
        mYesNoDialog.setButton(DialogInterface.BUTTON_POSITIVE, positiveButton, clickListener);
        mYesNoDialog.setButton(DialogInterface.BUTTON_NEGATIVE, negativeButton, clickListener);
        mYesNoDialog.setMessage(text);
        if (!mYesNoDialog.isShowing()) {
            mYesNoDialog.show();
        }
        return mYesNoDialog;
    }

    protected void checkPermission(List<String> permissionList, String message) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, permissionList)) {
            if (mDialog == null) {
                mDialog = new CommonDialog(this);
            }
            mDialog.setMessage(message);
            mDialog.setCanceledOnTouchOutside(false);
            if (!mDialog.isShowing()) {
                mDialog.show();
            }
            mDialog.setOnClickListener(new CommonDialog.OnClickListener() {
                @Override
                public void onConfirmClick(View view) {
                    PermissionUtils.getInstance().toSetting(BaseActivity.this);
                    mDialog.dismiss();
                }

                @Override
                public void onCancelClick(View view) {
                    mDialog.dismiss();
                }
            });
            mDialog.setCancelable(false);
        } else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        dismissDialog();
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
        }

    }

    protected <T> boolean isResponeSuccess(Response<ResObj> response, Context context) {
        if (response.code() != 200) {
            ToastUtil.getInstance().showToast(context,
                    response.message());
            return false;
        }
        if (response.body().getCode() != 0) {
            ToastUtil.getInstance().showToast(context,
                    response.body().getMsg());
            return false;
        }
        return true;
    }
}
